<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="标题">
  <van-icon slot="left" name="cross" @click="$router.back()" />
</van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="userFromRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="验证码"
        label="验证码"
        placeholder="请输入验证码"
        :rules="userFromRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="1000 * 5"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            type="primary"
            native-type="button"
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
export default {
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '13911111233',
        code: '246810'
      },
      userFromRules: {
        mobile: [
          {
            required: true,
            message: '手机号不可以为空'
          },
          {
            pattern: /^1[3|5|7|9]\d{9}/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不可以为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      isCountDownShow: false // 控制倒计时的显示和隐藏
    }
  },
  methods: {
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true
      })
      try {
        const { data: { data } } = await login(this.user)
        // console.log(data)
        this.$store.commit('setUser', data)
        this.$toast.success('登录成功') // 成功提示
        // this.$router.push('/')// 登录跳转
        this.$router.back()
      } catch (err) {
        if (err.response && err.response.status === 400) {
          this.$toast.fail('手机号或验证码不对') // 失败提示
        } else {
          this.$toast.fail('未知错误')
        }
      }
    },
    // 验证手机验证码
    async onSendSms () {
      // 1. 校验手机号
      try {
        console.log(this)
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log(err)
      }

      // 2. 验证通过，显示倒计时
      this.isCountDownShow = true

      // 3. 请求发送验证码
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    // width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}

/deep/ .van-button--primary {
  border-color: #1989fa !important;
}
</style>
